package cardwall.server;

import cardwall.shared.CardWallService;
import cardwall.shared.GTask;
import cardwall.shared.Task;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import simpleorm.dataset.SQuery;
import simpleorm.sessionjdbc.SSessionJdbc;
import simpleorm.utils.SException;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author $LastChangedBy: vogensen $
 * @version $Revision: 2 $
 */
public class CardWallServiceImpl extends RemoteServiceServlet implements CardWallService {
    public ArrayList<Task> getTaskList() {
        DataSource myds = new MyDataSource();
        SSessionJdbc session = SSessionJdbc.open(myds, "myds");
        try {
//            // drop and create tables
//            session.begin();
//            session.flush();
//            session.getDriver().dropTableNoError(STask.TASK.getTableName());
//            session.rawUpdateDB(session.getDriver().createTableSQL(STask.TASK));
//            session.commit();

            // get row
            session.begin();
            SQuery<STask> query = new SQuery(STask.TASK).descending(STask.ID);
            List<STask> sTaskList = session.query(query);
            ArrayList<Task> taskList = new ArrayList<Task>();
            for (STask sTask : sTaskList) {
                taskList.add(new GTask(sTask));
            }
            session.commit();
            return taskList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            SSessionJdbc.getThreadLocalSession().close();
        }
    }

    public Task createTask() {
        DataSource myds = new MyDataSource();
        SSessionJdbc session = SSessionJdbc.open(myds, "myds");
        try {
            // create row
            session.begin();
            Task task = session.createWithGeneratedKey(STask.TASK);
            task.setName("Task");
            task.setDescription("Description");
            task.setLeft(100);
            task.setTop(100);
            task = new GTask(task);
            session.commit();
            return task;
        } finally {
            SSessionJdbc.getThreadLocalSession().close();
        }
    }

    public Task updateTask(Task task) {
        DataSource myds = new MyDataSource();
        SSessionJdbc session = SSessionJdbc.open(myds, "myds");
        try {
            session.begin();
            SQuery<STask> query = new SQuery(STask.TASK).eq(STask.ID, task.getId());
            STask sTask = session.queryOnlyRecord(query);
            sTask.update(task);
            session.commit();
            return task;
        } finally {
            SSessionJdbc.getThreadLocalSession().close();
        }
    }

    static class MyDataSource implements DataSource {

        String dburl, dbUserName, dbPassword, dbDriver;

        MyDataSource() {
//            dburl = "jdbc:hsqldb:hsqlTempFiles";
            dburl = "jdbc:hsqldb:hsql://localhost/xdb";
            if (dburl == null) {
                throw new SException.Error("database.url property not found");
            }
            dburl = dburl.trim();
            dbUserName = "sa";
            dbPassword = "";
            dbDriver = "org.hsqldb.jdbcDriver";
            try {
                Class.forName(dbDriver);
            } catch (Exception ex) {
                throw new SException.Jdbc("Loading " + dbDriver, ex);
            }
        }

        public Connection getConnection() throws SQLException {
            Connection con = java.sql.DriverManager.getConnection(dburl, dbUserName, dbPassword);
            return con;
        }

        public Connection getConnection(String username, String password) throws SQLException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public PrintWriter getLogWriter() throws SQLException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public int getLoginTimeout() throws SQLException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setLogWriter(PrintWriter out) throws SQLException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setLoginTimeout(int seconds) throws SQLException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean isWrapperFor(Class<?> iface) throws SQLException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public <T> T unwrap(Class<T> iface) throws SQLException {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}
